# -*- coding:utf-8 -*-

## =====================================================================
## Copyright (C) 2007-2011  Jean-Philippe GOLAY
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
## =====================================================================


from Tkinter import *
import Pmw
from tkMessageBox import *

from Persistance import *

class ProductFamilyGUI(Toplevel):
	def __init__(self, parent=None, balloon=None, path=None, log=None):
		Toplevel.__init__(self, parent)
		self.parent=parent
		self.grab_set() 
		self.transient(self.master)
		self.resizable(width=False, height=False)
		self.title(u"Famille de produit")
		self.balloon=balloon
		self.imgSave=PhotoImage(file="img/save_icon.gif")
		self.imgDelete=PhotoImage(file="img/delete_icon.gif")
		self.path=path
		self.log=log
		# Afficher un icon personnalisé
		try:
			if os.name =="posix":
				img = PhotoImage(file=self.path+"/icons/Product-icon.gif")
				self.tk.call("wm", "iconphoto", self._w, img)
			else:
				self.iconbitmap(default=self.path+"/icons/Product-icon.gif")
		except TclError:
			self.log.warning("Le window manager ne supporte pas l'icône que je veux lui mettre...")
		self.log.info("\tProductFamilyGUI\tStart")
		self.makeProductFamily()

	# DEBUT de la gestion du module ProductFamily --------------------------------------------------------------------------------------------------
	def clearFieldProductFamily(self):
		# Mise a zéro des champs de la Famille de Produit
		self.productFamilyValues[0].set("")
		self.productFamilyValues[1].set("")

	def selectProductFamily(self, event):
		if self.listProductFamily.get(0)!="":
			result = ProductFamily.select(ProductFamily.q.id==self.idListProductFamily[int(self.listProductFamily.curselection()[0])])
			for row in result:
				self.selectedProductFamily = row
			self.productFamilyValues[0].set(self.selectedProductFamily.code)
			self.productFamilyValues[1].set(self.selectedProductFamily.designation)
			# activation du bouton supprimer
			self.deleteProductFamilyBt.config(state=NORMAL)

	def searchProductFamily(self, event=None):
		if self.searchProductFamilyValue.get() != "":
			self.listProductFamily.delete(0,END)
			self.idListProductFamily = []
			result = ProductFamily.select(ProductFamily.q.designation.startswith(self.searchProductFamilyValue.get().encode("utf-8")), orderBy=ProductFamily.q.designation)
			for row in result:
				self.listProductFamily.insert(END, row.designation+" - "+row.code)
				self.idListProductFamily.append(row.id)
		else:
			self.listProductFamily.delete(0,END)
			self.idListProductFamily = []
			result = ProductFamily.select(orderBy=ProductFamily.q.designation)
			for row in result:
				self.listProductFamily.insert(END, row.designation+" - "+row.code)
				self.idListProductFamily.append(row.id)

	def makeProductFamily(self):
		leftFrame = Frame(self)
		# Fiche ProductFamily  (présentation colonne de gauche)
		Label(leftFrame, text=u"Famille de Produit").pack(side=TOP, fill=X)
		# Création des champs a renseigner pour un object ProductFamily
		productFamilyLabels = (u"Code", u"Déscription")
		self.productFamilyValues = [StringVar(), StringVar()]
		count=0
		for name in productFamilyLabels:
			f = Frame(leftFrame)
			Label(f, width=12, text=name, anchor=W).pack(side=LEFT)
			Entry(f, textvariable=self.productFamilyValues[count], width=33).pack(side=LEFT, fill=X)
			f.pack(side=TOP, fill=X)
			count+=1
		# Boutons enregistrement (new ou modify) et suppression
		s = Frame(leftFrame) 
		self.saveProductFamilyBt=Button(s, image=self.imgSave, command=self.createProductFamily)
		self.saveProductFamilyBt.bind("<Return>", self.createProductFamily)
		self.saveProductFamilyBt.pack(side=LEFT)
		self.balloon.bind(self.saveProductFamilyBt, u"Enregistrer")
		self.deleteProductFamilyBt=Button(s, image=self.imgDelete, command=self.deleteProductFamily, state=DISABLED)
		self.deleteProductFamilyBt.bind("<Return>", self.deleteProductFamily)
		self.deleteProductFamilyBt.pack(side=LEFT)
		self.balloon.bind(self.deleteProductFamilyBt, u"Supprimer")
		s.pack(side=TOP, fill=X)
		leftFrame.pack(side=LEFT, fill=X)
		# Liste des Discount existants (présentation colonne de droite)
		#~ rightFrame = Frame(self) 
		Label(leftFrame, text=u"Liste des Familles de Produit").pack(side=TOP, fill=X)
		self.searchProductFamilyValue = StringVar()
		self.searchProductFamilyEt = Entry(leftFrame,  width=45, textvariable=self.searchProductFamilyValue, relief=GROOVE)
		self.searchProductFamilyEt.bind("<KeyRelease>", self.searchProductFamily)
		self.searchProductFamilyEt.pack(side=TOP, fill=X)
		self.balloon.bind(self.searchProductFamilyEt, u"Saisir les premières lettres d'une Famille de Produit\npour une recherche plus précise,\nla liste est classée par date de début.")
		s = Frame(leftFrame) 
		scrollbar = Scrollbar(s)
		scrollbar.pack(side=RIGHT, fill=Y)
		self.listProductFamily = Listbox(s)
		self.listProductFamily.config(yscrollcommand=scrollbar.set, width=43, selectmode=SINGLE, height=10)
		self.listProductFamily.bind("<ButtonRelease-1>",self.selectProductFamily)
		scrollbar.config(command=self.listProductFamily.yview)
		self.listProductFamily.pack(side=TOP, fill=X)
		self.balloon.bind(self.listProductFamily, u"Séléctionner une Famille de Produit\npour la visualiser ou la modifier.")
		s.pack(side=TOP, fill=X)
		#~ rightFrame.pack(side=RIGHT, fill=X)
		leftFrame.pack(side=LEFT, fill=X)
		# Initialisation
		self.clearFieldProductFamily()
		self.searchProductFamily()

	def verifyProductFamily(self):
		if self.productFamilyValues[0].get() == "":
			showerror(u"Erreur de saisie", u"Le champ Code est obligatoire !\nMerci de le compléter.")
			return False
		elif self.productFamilyValues[1].get() == "":
			showerror(u"Erreur de saisie", u"Le champ Déscription est obligatoire !\nMerci de le compléter.")
			return False
		else:
			return True

	def createProductFamily(self, event=None):
		if self.verifyProductFamily():
			if  self.deleteProductFamilyBt.config()["state"][4] == "normal":
				self.modifyProductFamily()
			else:
				if askyesno(u"Enregistrement", u"Souhaitez-vous enregistrer %s comme une nouvelle Famille de Produit ?\n" % self.productFamilyValues[1].get()):
					ProductFamily(code=self.productFamilyValues[0].get().encode("utf-8"),
						designation=self.productFamilyValues[1].get().encode("utf-8"),
						createDate=datetime.datetime.now()
					)
					self.searchProductFamily()
				self.clearFieldProductFamily()

	def modifyProductFamily(self, event=None): 
		if askyesno(u"Modifier", u"Souhaitez-vous modifier la Famille de Produit %s ?" % self.selectedProductFamily.designation):
			self.selectedProductFamily.code=self.productFamilyValues[0].get().encode("utf-8")
			self.selectedProductFamily.designation=self.productFamilyValues[1].get().encode("utf-8")
			self.searchProductFamily()
		self.deleteProductFamilyBt.config(state=DISABLED)
		self.clearFieldProductFamily()

	def deleteProductFamily(self, event=None): 
		if askyesno(u"Suppression", u"Souhaitez-vous supprimer la Famille de Produit %s ?\nCette opération est irréversible !" % self.selectedProductFamily.designation):
			ProductFamily.delete(self.selectedProductFamily.id)
			self.searchProductFamily()            
		self.deleteProductFamilyBt.config(state=DISABLED)
		self.clearFieldProductFamily()
	# FIN de la gestion du module ProductFamily ------------------------------------------------------------------------------------------------------

class ProductFamilyListGUI(Toplevel):
	def __init__(self, parent=None, balloon=None, field=None, index=None, listObject=None, path=None, log=None):
		Toplevel.__init__(self, parent)
		self.parent=parent
		self.grab_set() 
		self.transient(self.master)
		self.resizable(width=False, height=False)
		self.title(u"Liste des Familles de Produits")
		self.balloon=balloon
		self.imgNew=PhotoImage(file="img/add_alternative_icon.gif")
		self.field=field
		self.index=index
		self.listObject=listObject
		self.path=path
		self.log=log
		# Afficher un icon personnalisé
		try:
			if os.name =="posix":
				img = PhotoImage(file=self.path+"/icons/Product-icon.gif")
				self.tk.call("wm", "iconphoto", self._w, img)
			else:
				self.iconbitmap(default=self.path+"/icons/Product-icon.gif")
		except TclError:
			self.log.warning("Le window manager ne supporte pas l'icône que je veux lui mettre...")
		self.log.info("\tProductFamilyListGUI\tStart")
		self.makeListProductFamily()

	def selectListProductFamily(self, event):
		if self.listListProductFamily.get(0)!="":
			result = ProductFamily.select(ProductFamily.q.id==self.idListListProductFamily[int(self.listListProductFamily.curselection()[0])])
			for row in result:
				self.listObject[self.index]=row
				self.field.set(row.designation+" - "+row .code)
				self.destroy()

	def searchListProductFamily(self, event=None):
		if self.searchListProductFamilyValue.get() != "":
			self.listListProductFamily.delete(0,END)
			self.idListListProductFamily = []
			result = ProductFamily.select(ProductFamily.q.designation.startswith(self.searchListProductFamilyValue.get().encode("utf-8")), orderBy=ProductFamily.q.designation)
			for row in result:
				self.listListProductFamily.insert(END, row.designation+" - "+row.code)
				self.idListListProductFamily.append(row.id)
		else:
			self.listListProductFamily.delete(0,END)
			self.idListListProductFamily = []
			result = ProductFamily.select(orderBy=ProductFamily.q.designation)
			for row in result:
				self.listListProductFamily.insert(END, row.designation+" - "+row.code)
				self.idListListProductFamily.append(row.id)

	def makeListProductFamily(self):
		leftFrame = Frame(self)
		Label(leftFrame, text=u"Familles de Produits").pack(side=TOP, fill=X)
		self.searchListProductFamilyValue = StringVar()
		s = Frame(leftFrame) 
		self.searchListProductFamilyEt = Entry(s,  width=32, textvariable=self.searchListProductFamilyValue, relief=GROOVE)
		self.searchListProductFamilyEt.bind("<KeyRelease>", self.searchListProductFamily)
		self.searchListProductFamilyEt.pack(side=LEFT, fill=X)
		self.balloon.bind(self.searchListProductFamilyEt, u"Saisir les premières lettres d'une Famille de Produit\npour une recherche plus précise,\nla liste est classée par désignation.")
		newProductFamilyBT=Button(s, image=self.imgNew, command=self.ctrlProductFamily)
		newProductFamilyBT.pack(side=RIGHT)
		self.balloon.bind(newProductFamilyBT, u"Créer une nouvelle famille de produit")
		s.pack(side=TOP, fill=X)
		s = Frame(leftFrame) 
		scrollbar = Scrollbar(s)
		scrollbar.pack(side=RIGHT, fill=Y)
		self.listListProductFamily = Listbox(s)
		self.listListProductFamily.config(yscrollcommand=scrollbar.set,width=33, selectmode=SINGLE, height=16)
		self.listListProductFamily.bind("<ButtonRelease-1>",self.selectListProductFamily)
		scrollbar.config(command=self.listListProductFamily.yview)
		self.listListProductFamily.pack(side=TOP, fill=X)
		self.balloon.bind(self.listListProductFamily, u"Séléctionner une Famille de Produit.")
		s.pack(side=TOP, fill=X)
		leftFrame.pack()
		self.searchListProductFamily()
		
	def ctrlProductFamily(self):
		ProductFamilyGUI(balloon=self.balloon, log=self.log, path=self.path)

class ServiceFamilyGUI(Toplevel):
	def __init__(self, parent=None, balloon=None, path=None, log=None):
		Toplevel.__init__(self, parent)
		self.parent=parent
		self.grab_set() 
		self.transient(self.master)
		self.resizable(width=False, height=False)
		self.title(u"Famille de service")
		self.balloon=balloon
		self.imgSave=PhotoImage(file="img/save_icon.gif")
		self.imgDelete=PhotoImage(file="img/delete_icon.gif")
		self.path=path
		self.log=log
		# Afficher un icon personnalisé
		try:
			if os.name =="posix":
				img = PhotoImage(file=self.path+"/icons/Service-icon.gif")
				self.tk.call("wm", "iconphoto", self._w, img)
			else:
				self.iconbitmap(default=self.path+"/icons/Service-icon.gif")
		except TclError:
			self.log.warning("Le window manager ne supporte pas l'icône que je veux lui mettre...")
		self.log.info("\tServiceFamilyGUI\tStart")
		self.makeServiceFamily()

	# DEBUT de la gestion du module ServiceFamily --------------------------------------------------------------------------------------------------
	def clearFieldServiceFamily(self):
		# Mise a zéro des champs de la fiche ServiceFamily
		self.serviceFamilyValues[0].set("")
		self.serviceFamilyValues[1].set("")

	def selectServiceFamily(self, event):
		if self.listServiceFamily.get(0)!="":
			result = ServiceFamily.select(ServiceFamily.q.id==self.idListServiceFamily[int(self.listServiceFamily.curselection()[0])])
			for row in result:
				self.selectedServiceFamily = row
			self.serviceFamilyValues[0].set(self.selectedServiceFamily.code)
			self.serviceFamilyValues[1].set(self.selectedServiceFamily.designation)
			# activation du bouton supprimer
			self.deleteServiceFamilyBt.config(state=NORMAL)

	def searchServiceFamily(self, event=None):
		if self.searchServiceFamilyValue.get() != "":
			self.listServiceFamily.delete(0,END)
			self.idListServiceFamily = []
			result = ServiceFamily.select(ServiceFamily.q.designation.startswith(self.searchServiceFamilyValue.get().encode("utf-8")), orderBy=ServiceFamily.q.designation)
			for row in result:
				self.listServiceFamily.insert(END, row.designation+" - "+row.code)
				self.idListServiceFamily.append(row.id)
		else:
			self.listServiceFamily.delete(0,END)
			self.idListServiceFamily = []
			result = ServiceFamily.select(orderBy=ServiceFamily.q.designation)
			for row in result:
				self.listServiceFamily.insert(END, row.designation+" - "+row.code)
				self.idListServiceFamily.append(row.id)

	def makeServiceFamily(self):
		leftFrame = Frame(self)
		# Fiche ProductFamily  (présentation colonne de gauche)
		Label(leftFrame, text=u"Famille de Service").pack(side=TOP, fill=X)
		# Création des champs a renseigner pour un object ServiceFamily
		serviceFamilyLabels = (u"Code", u"Déscription")
		self.serviceFamilyValues = [StringVar(), StringVar()]
		count=0
		for name in serviceFamilyLabels:
			f = Frame(leftFrame)
			Label(f, width=12, text=name, anchor=W).pack(side=LEFT)
			Entry(f, textvariable=self.serviceFamilyValues[count], width=33).pack(side=LEFT, fill=X)
			f.pack(side=TOP, fill=X)
			count+=1
		# Boutons enregistrement (new ou modify) et suppression
		s = Frame(leftFrame) 
		self.saveServiceFamilyBt=Button(s, image=self.imgSave, command=self.createServiceFamily)
		self.saveServiceFamilyBt.bind("<Return>", self.createServiceFamily)
		self.saveServiceFamilyBt.pack(side=LEFT)
		self.balloon.bind(self.saveServiceFamilyBt, u"Enregistrer")
		self.deleteServiceFamilyBt=Button(s, image=self.imgDelete, command=self.deleteServiceFamily, state=DISABLED)
		self.deleteServiceFamilyBt.bind("<Return>", self.deleteServiceFamily)
		self.deleteServiceFamilyBt.pack(side=LEFT)
		self.balloon.bind(self.deleteServiceFamilyBt, u"Supprimer")
		s.pack(side=TOP, fill=X)        
		# Liste des ServiceFamily existants (présentation colonne de droite)
		#~ rightFrame = Frame(self) 
		Label(leftFrame, text=u"Liste des Familles de Service").pack(side=TOP, fill=X)
		self.searchServiceFamilyValue = StringVar()
		self.searchServiceFamilyEt = Entry(leftFrame,  width=43, textvariable=self.searchServiceFamilyValue, relief=GROOVE)
		self.searchServiceFamilyEt.bind("<KeyRelease>", self.searchServiceFamily)
		self.searchServiceFamilyEt.pack(side=TOP, fill=X)
		self.balloon.bind(self.searchServiceFamilyEt, u"Saisir les premières lettres d'une Famille de Service\npour une recherche plus précise,\nla liste est classée par date de début.")
		s = Frame(leftFrame) 
		scrollbar = Scrollbar(s)
		scrollbar.pack(side=RIGHT, fill=Y)
		self.listServiceFamily = Listbox(s)
		self.listServiceFamily.config(yscrollcommand=scrollbar.set, width=45, selectmode=SINGLE, height=10)
		self.listServiceFamily.bind("<ButtonRelease-1>",self.selectServiceFamily)
		scrollbar.config(command=self.listServiceFamily.yview)
		self.listServiceFamily.pack(side=TOP, fill=X)
		self.balloon.bind(self.listServiceFamily, u"Séléctionner une Famille de Service\npour la visualiser ou la modifier.")
		s.pack(side=TOP, fill=X)
		#~ rightFrame.pack(side=RIGHT, fill=X)
		leftFrame.pack(side=LEFT, fill=X)
		# Initialisation
		self.clearFieldServiceFamily()
		self.searchServiceFamily()

	def verifyServiceFamily(self):
		if self.serviceFamilyValues[0].get() == "":
			showerror(u"Erreur de saisie", u"Le champ Code est obligatoire !\nMerci de le compléter.")
			return False
		elif self.serviceFamilyValues[1].get() == "":
			showerror(u"Erreur de saisie", u"Le champ Déscription est obligatoire !\nMerci de le compléter.")
			return False
		else:
			return True

	def createServiceFamily(self, event=None):
		if self.verifyServiceFamily():
			if  self.deleteServiceFamilyBt.config()["state"][4] == "normal":
				self.modifyServiceFamily()
			else:
				if askyesno(u"Enregistrement", u"Souhaitez-vous enregistrer %s comme une nouvelle Famille de Service ?\n" % self.serviceFamilyValues[1].get()):
					ServiceFamily(code=self.serviceFamilyValues[0].get().encode("utf-8"),
						designation=self.serviceFamilyValues[1].get().encode("utf-8"),
						createDate=datetime.datetime.now()
					)
					self.searchServiceFamily()
				self.clearFieldServiceFamily()

	def modifyServiceFamily(self, event=None): 
		if askyesno(u"Modifier", u"Souhaitez-vous modifier la Famille de Service %s ?" % self.selectedServiceFamily.designation):
			self.selectedServiceFamily.code=self.serviceFamilyValues[0].get().encode("utf-8")
			self.selectedServiceFamily.designation=self.serviceFamilyValues[1].get().encode("utf-8")
			self.searchServiceFamily()
		self.deleteServiceFamilyBt.config(state=DISABLED)
		self.clearFieldServiceFamily()

	def deleteServiceFamily(self, event=None): 
		if askyesno(u"Suppression", u"Souhaitez-vous supprimer la Famille de Service %s ?\nCette opération est irréversible !" % self.selectedServiceFamily.designation):
			ServiceFamily.delete(self.selectedServiceFamily.id)
			self.searchServiceFamily()            
		self.deleteServiceFamilyBt.config(state=DISABLED)
		self.clearFieldServiceFamily()
	# FIN de la gestion du module ServiceFamily ------------------------------------------------------------------------------------------------------

class ServiceFamilyListGUI(Toplevel):
	def __init__(self, parent=None, balloon=None, field=None, index=None, listObject=None, path=None, log=None):
		Toplevel.__init__(self, parent)
		self.parent=parent
		self.grab_set()
		self.transient(self.master)
		self.resizable(width=False, height=False)
		self.title(u"Liste des Familles de Services")
		self.balloon=balloon
		self.imgNew=PhotoImage(file="img/add_alternative_icon.gif")
		self.field=field
		self.index=index
		self.listObject=listObject
		self.path=path
		self.log=log
		# Afficher un icon personnalisé
		try:
			if os.name =="posix":
				img = PhotoImage(file=self.path+"/icons/Service-icon.gif")
				self.tk.call("wm", "iconphoto", self._w, img)
			else:
				self.iconbitmap(default=self.path+"/icons/Service-icon.gif")
		except TclError:
			self.log.warning("Le window manager ne supporte pas l'icône que je veux lui mettre...")
		self.log.info("\tServiceFamilyListGUI\tStart")
		self.makeListServiceFamily()

	def selectListServiceFamily(self, event):
		if self.listListServiceFamily.get(0)!="":
			result = ServiceFamily.select(ServiceFamily.q.id==self.idListListServiceFamily[int(self.listListServiceFamily.curselection()[0])])
			for row in result:
				self.listObject[self.index]=row
				self.field.set(row.designation+" - "+row .code)
				self.destroy()

	def searchListServiceFamily(self, event=None):
		if self.searchListServiceFamilyValue.get() != "":
			self.listListServiceFamily.delete(0,END)
			self.idListListServiceFamily = []
			result = ServiceFamily.select(ServiceFamily.q.designation.startswith(self.searchListServiceFamilyValue.get().encode("utf-8")), orderBy=ServiceFamily.q.designation)
			for row in result:
				self.listListServiceFamily.insert(END, row.designation+" - "+row.code)
				self.idListListServiceFamily.append(row.id)
		else:
			self.listListServiceFamily.delete(0,END)
			self.idListListServiceFamily = []
			result = ServiceFamily.select(orderBy=ServiceFamily.q.designation)
			for row in result:
				self.listListServiceFamily.insert(END, row.designation+" - "+row.code)
				self.idListListServiceFamily.append(row.id)

	def makeListServiceFamily(self):
		leftFrame = Frame(self)
		Label(leftFrame, text=u"Familles de Services").pack(side=TOP, fill=X)
		self.searchListServiceFamilyValue = StringVar()
		s = Frame(leftFrame) 
		self.searchListServiceFamilyEt = Entry(s,  width=32, textvariable=self.searchListServiceFamilyValue, relief=GROOVE)
		self.searchListServiceFamilyEt.bind("<KeyRelease>", self.searchListServiceFamily)
		self.searchListServiceFamilyEt.pack(side=LEFT, fill=X)
		self.balloon.bind(self.searchListServiceFamilyEt, u"Saisir les premières lettres d'une Famille de Service\npour une recherche plus précise,\nla liste est classée par désignation.")
		newServiceFamilyBt=Button(s, image=self.imgNew, command=self.ctrlServiceFamily)
		newServiceFamilyBt.pack(side=RIGHT)
		self.balloon.bind(newServiceFamilyBt, u"Créer une nouvelle famille de service")
		s.pack(side=TOP, fill=X)
		s = Frame(leftFrame) 
		scrollbar = Scrollbar(s)
		scrollbar.pack(side=RIGHT, fill=Y)
		self.listListServiceFamily = Listbox(s)
		self.listListServiceFamily.config(yscrollcommand=scrollbar.set,width=33, selectmode=SINGLE, height=16)
		self.listListServiceFamily.bind("<ButtonRelease-1>",self.selectListServiceFamily)
		scrollbar.config(command=self.listListServiceFamily.yview)
		self.listListServiceFamily.pack(side=TOP, fill=X)
		self.balloon.bind(self.listListServiceFamily, u"Séléctionner une Famille de Service.")
		s.pack(side=TOP, fill=X)
		leftFrame.pack()
		self.searchListServiceFamily()
		
	def ctrlServiceFamily(self):
		ServiceFamilyGUI(balloon=self.balloon, log=self.log, path=self.path)

class ItemFamilyGUI(Toplevel):
	def __init__(self, parent=None, balloon=None, path=None, log=None):
		Toplevel.__init__(self, parent)
		self.parent=parent
		self.grab_set() 
		self.transient(self.master)
		self.resizable(width=False, height=False)
		self.title(u"Famille d'Articles")
		self.balloon=balloon
		self.imgSave=PhotoImage(file="img/save_icon.gif")
		self.imgDelete=PhotoImage(file="img/delete_icon.gif")
		self.path=path
		self.log=log
		# Afficher un icon personnalisé
		try:
			if os.name =="posix":
				img = PhotoImage(file=self.path+"/icons/Item-icon.gif")
				self.tk.call("wm", "iconphoto", self._w, img)
			else:
				self.iconbitmap(default=self.path+"/icons/Item-icon.gif")
		except TclError:
			self.log.warning("Le window manager ne supporte pas l'icône que je veux lui mettre...")
		self.log.info("\tItemFamilyGUI\tStart")
		self.makeItemFamily()

	# DEBUT de la gestion du module ItemFamily --------------------------------------------------------------------------------------------------
	def clearFieldItemFamily(self):
		# Mise a zéro des champs de la fiche ItemFamily
		self.itemFamilyValues[0].set("")
		self.itemFamilyValues[1].set("")

	def selectItemFamily(self, event):
		if self.listItemFamily.get(0)!="":
			result = ItemFamily.select(ItemFamily.q.id==self.idListItemFamily[int(self.listItemFamily.curselection()[0])])
			for row in result:
				self.selectedItemFamily = row
			self.itemFamilyValues[0].set(self.selectedItemFamily.code)
			self.itemFamilyValues[1].set(self.selectedItemFamily.designation)
			# activation du bouton supprimer
			self.deleteItemFamilyBt.config(state=NORMAL)

	def searchItemFamily(self, event=None):
		if self.searchItemFamilyValue.get() != "":
			self.listItemFamily.delete(0,END)
			self.idListItemFamily = []
			result = ItemFamily.select(ItemFamily.q.designation.startswith(self.searchItemFamilyValue.get().encode("utf-8")), orderBy=ItemFamily.q.designation)
			for row in result:
				self.listItemFamily.insert(END, row.designation+" - "+row.code)
				self.idListItemFamily.append(row.id)
		else:
			self.listItemFamily.delete(0,END)
			self.idListItemFamily = []
			result = ItemFamily.select(orderBy=ItemFamily.q.designation)
			for row in result:
				self.listItemFamily.insert(END, row.designation+" - "+row.code)
				self.idListItemFamily.append(row.id)

	def makeItemFamily(self):
		leftFrame = Frame(self)
		# Fiche ItemFamily  (présentation colonne de gauche)
		Label(leftFrame, text=u"Famille d'Articles").pack(side=TOP, fill=X)
		# Création des champs a renseigner pour un object ItemFamily
		itemFamilyLabels = (u"Code", u"Déscription")
		self.itemFamilyValues = [StringVar(), StringVar()]
		count=0
		for name in itemFamilyLabels:
			f = Frame(leftFrame)
			Label(f, width=12, text=name, anchor=W).pack(side=LEFT)
			Entry(f, textvariable=self.itemFamilyValues[count], width=33).pack(side=LEFT, fill=X)
			f.pack(side=TOP, fill=X)
			count+=1
		# Boutons enregistrement (new ou modify) et suppression
		s = Frame(leftFrame) 
		self.saveItemFamilyBt=Button(s, image=self.imgSave, command=self.createItemFamily)
		self.saveItemFamilyBt.bind("<Return>", self.createItemFamily)
		self.saveItemFamilyBt.pack(side=LEFT)
		self.balloon.bind(self.saveItemFamilyBt, u"Enregistrer")
		self.deleteItemFamilyBt=Button(s, image=self.imgDelete, command=self.deleteItemFamily, state=DISABLED)
		self.deleteItemFamilyBt.bind("<Return>", self.deleteItemFamily)
		self.deleteItemFamilyBt.pack(side=LEFT)
		self.balloon.bind(self.deleteItemFamilyBt, u"Supprimer")
		s.pack(side=TOP, fill=X)
		# Liste des ItemFamily existants (présentation colonne de droite)
		#~ rightFrame = Frame(self) 
		Label(leftFrame, text=u"Liste des Familles d'Articles").pack(side=TOP, fill=X)
		self.searchItemFamilyValue = StringVar()
		self.searchItemFamilyEt = Entry(leftFrame,  width=43, textvariable=self.searchItemFamilyValue, relief=GROOVE)
		self.searchItemFamilyEt.bind("<KeyRelease>", self.searchItemFamily)
		self.searchItemFamilyEt.pack(side=TOP, fill=X)
		self.balloon.bind(self.searchItemFamilyEt, u"Saisir les premières lettres d'une Famille d'Articles\npour une recherche plus précise,\nla liste est classée par date de début.")
		s = Frame(leftFrame) 
		scrollbar = Scrollbar(s)
		scrollbar.pack(side=RIGHT, fill=Y)
		self.listItemFamily = Listbox(s)
		self.listItemFamily.config(yscrollcommand=scrollbar.set, width=45, selectmode=SINGLE, height=10)
		self.listItemFamily.bind("<ButtonRelease-1>",self.selectItemFamily)
		scrollbar.config(command=self.listItemFamily.yview)
		self.listItemFamily.pack(side=TOP, fill=X)
		self.balloon.bind(self.listItemFamily, u"Séléctionner une Famille d'Articles\npour la visualiser ou la modifier.")
		s.pack(side=TOP, fill=X)
		#~ rightFrame.pack(side=RIGHT, fill=X)
		leftFrame.pack(side=LEFT, fill=X)
		# Initialisation
		self.clearFieldItemFamily()
		self.searchItemFamily()

	def verifyItemFamily(self):
		if self.itemFamilyValues[0].get() == "":
			showerror(u"Erreur de saisie", u"Le champ Code est obligatoire !\nMerci de le compléter.")
			return False
		elif self.itemFamilyValues[1].get() == "":
			showerror(u"Erreur de saisie", u"Le champ Déscription est obligatoire !\nMerci de le compléter.")
			return False
		else:
			return True

	def createItemFamily(self, event=None):
		if self.verifyItemFamily():
			if  self.deleteItemFamilyBt.config()["state"][4] == "normal":
				self.modifyItemFamily()
			else:
				if askyesno(u"Enregistrement", u"Souhaitez-vous enregistrer %s comme une nouvelle Famille d'Articles ?\n" % self.itemFamilyValues[1].get()):
					ItemFamily(code=self.itemFamilyValues[0].get().encode("utf-8"),
						designation=self.itemFamilyValues[1].get().encode("utf-8"),
						createDate=datetime.datetime.now()
					)
					self.searchItemFamily()
				self.clearFieldItemFamily()

	def modifyItemFamily(self, event=None): 
		if askyesno(u"Modifier", u"Souhaitez-vous modifier la Famille d'Articles %s ?" % self.selectedItemFamily.designation):
			self.selectedItemFamily.code=self.itemFamilyValues[0].get().encode("utf-8")
			self.selectedItemFamily.designation=self.itemFamilyValues[1].get().encode("utf-8")
			self.searchItemFamily()
		self.deleteItemFamilyBt.config(state=DISABLED)
		self.clearFieldItemFamily()

	def deleteItemFamily(self, event=None): 
		if askyesno(u"Suppression", u"Souhaitez-vous supprimer la Famille d'Articles %s ?\nCette opération est irréversible !" % self.selectedItemFamily.designation):
			ItemFamily.delete(self.selectedItemFamily.id)
			self.searchItemFamily()            
		self.deleteItemFamilyBt.config(state=DISABLED)
		self.clearFieldItemFamily()
	# FIN de la gestion du module ItemFamily -------------------------------------------------------------------------------------------------------------

class ItemFamilyListGUI(Toplevel):
	def __init__(self, parent=None, balloon=None, field=None, index=None, listObject=None, path=None, log=None):
		Toplevel.__init__(self, parent)
		self.parent=parent
		self.grab_set() 
		self.transient(self.master)
		self.resizable(width=False, height=False)
		self.title(u"Liste des Familles d'Articles")
		self.balloon=balloon
		self.imgNew=PhotoImage(file="img/add_alternative_icon.gif")
		self.field=field
		self.index=index
		self.listObject=listObject
		self.path=path
		self.log=log
		# Afficher un icon personnalisé
		try:
			if os.name =="posix":
				img = PhotoImage(file=self.path+"/icons/Item-icon.gif")
				self.tk.call("wm", "iconphoto", self._w, img)
			else:
				self.iconbitmap(default=self.path+"/icons/Item-icon.gif")
		except TclError:
			self.log.warning("Le window manager ne supporte pas l'icône que je veux lui mettre...")
		self.log.info("\tItemFamilyListGUI\tStart")
		self.makeListItemFamily()

	def selectListItemFamily(self, event):
		if self.listListItemFamily.get(0)!="":
			result = ItemFamily.select(ItemFamily.q.id==self.idListListItemFamily[int(self.listListItemFamily.curselection()[0])])
			for row in result:
				self.listObject[self.index]=row
				self.field.set(row.designation+" - "+row .code)
				self.destroy()

	def searchListItemFamily(self, event=None):
		if self.searchListItemFamilyValue.get() != "":
			self.listListItemFamily.delete(0,END)
			self.idListListItemFamily = []
			result = ItemFamily.select(ItemFamily.q.designation.startswith(self.searchListItemFamilyValue.get().encode("utf-8")), orderBy=ItemFamily.q.designation)
			for row in result:
				self.listListItemFamily.insert(END, row.designation+" - "+row.code)
				self.idListListItemFamily.append(row.id)
		else:
			self.listListItemFamily.delete(0,END)
			self.idListListItemFamily = []
			result = ItemFamily.select(orderBy=ItemFamily.q.designation)
			for row in result:
				self.listListItemFamily.insert(END, row.designation+" - "+row.code)
				self.idListListItemFamily.append(row.id)

	def makeListItemFamily(self):
		leftFrame = Frame(self)
		Label(leftFrame, text=u"Familles d'Articles").pack(side=TOP, fill=X)
		self.searchListItemFamilyValue = StringVar()
		s = Frame(leftFrame) 
		self.searchListItemFamilyEt = Entry(s,  width=32, textvariable=self.searchListItemFamilyValue, relief=GROOVE)
		self.searchListItemFamilyEt.bind("<KeyRelease>", self.searchListItemFamily)
		self.searchListItemFamilyEt.pack(side=LEFT, fill=X)
		self.balloon.bind(self.searchListItemFamilyEt, u"Saisir les premières lettres d'une Famille d'Articles\npour une recherche plus précise,\nla liste est classée par désignation.")
		newItemFamilyBt=Button(s, image=self.imgNew, command=self.ctrlItemFamily, relief=FLAT)
		newItemFamilyBt.pack(side=RIGHT)
		self.balloon.bind(newItemFamilyBt, u"Créer une nouvelle famille d'article")
		s.pack(side=TOP, fill=X)
		s = Frame(leftFrame) 
		scrollbar = Scrollbar(s)
		scrollbar.pack(side=RIGHT, fill=Y)
		self.listListItemFamily = Listbox(s)
		self.listListItemFamily.config(yscrollcommand=scrollbar.set,width=33, selectmode=SINGLE, height=16)
		self.listListItemFamily.bind("<ButtonRelease-1>",self.selectListItemFamily)
		scrollbar.config(command=self.listListItemFamily.yview)
		self.listListItemFamily.pack(side=TOP, fill=X)
		self.balloon.bind(self.listListItemFamily, u"Séléctionner une Famille d'Articles.")
		s.pack(side=TOP, fill=X)
		leftFrame.pack()
		self.searchListItemFamily()

	def ctrlItemFamily(self):
		ItemFamilyGUI(balloon=self.balloon, log=self.log, path=self.path)